'use strict';
//获取数据库的引用
const db = uniCloud.database()
const $ = db.command.aggregate
exports.main = async (event, context) => {
	//获取label表的值
	const {
		user_id,
		type
	} = event
	let matchObk = {}
	if(type !=='all'){
			matchObk={
				current:true
			}
	}
	//拿到用户表
	let  userinfo = await db.collection('user').doc(user_id).get()
	//把对象从数组中拿出
	userinfo = userinfo.data[0];
	// let label = await db.collection('label').get();
	let label = await db.collection('label')
	.aggregate()
	//addFields 添加新字段
	.addFields({
		current:$.in(['$_id',$.ifNull([userinfo.label_ids,[]])])
	})
	.match(matchObk)
	.end()
	
	//返回数据给客户端
	return {
		code:200,
		msg:"数据请求成功",
		data:label.data
	}
};
